package com.wbf.demo.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wbf.demo.security.exception.ErrorMsg;
import com.wbf.demo.util.ServletTool;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.wbf.demo.common.CommonErrorMsgs.*;


/**
 * 异常处理
 * @author guangdao
 *
 */
public class JsonAuthFailureHandler implements AuthenticationFailureHandler{
	private final ObjectMapper objectMapper;
	
	public JsonAuthFailureHandler(ObjectMapper objectMapper){
		this.objectMapper = objectMapper;
	}
	
	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
		ErrorMsg errorMsg;
		if(exception instanceof UsernameNotFoundException) {
			response.setStatus(ILLEGAL_PARAM.getStatus().value());
			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "用户名不存在");

//		} else if(exception instanceof IllegalUsernameFormatFormatException){
//			response.setStatus(ILLEGAL_PARAM.getStatus().value());
//			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "用户名格式错误");
//		} else if(exception instanceof IllegalPasswordFormatException){
//			response.setStatus(ILLEGAL_PARAM.getStatus().value());
//			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "密码格式错误");
//		} else if(exception instanceof BadCredentialsException){
//			response.setStatus(ILLEGAL_PARAM.getStatus().value());
//			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "用户名或密码错误");
//		}else if(exception instanceof IllegalVerificationCodeFormatException){
//			response.setStatus(ILLEGAL_PARAM.getStatus().value());
//			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "验证码格式错误");
//		}else if(exception instanceof WrongUserNameException){
//			response.setStatus(ILLEGAL_PARAM.getStatus().value());
//			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "手机号已存在");
//		}else if(exception instanceof ThirdPartyLoginException){
//			response.setStatus(ILLEGAL_PARAM.getStatus().value());
//			errorMsg = new ErrorMsg(ERROR_THIRDPARTY, exception.getMessage());
//		}else if (exception instanceof WrongNickNameException) {
//			response.setStatus(ILLEGAL_PARAM.getStatus().value());
//			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "用户昵称已存在");
//		}else if (exception instanceof IllegalDeviceNoFormatException) {
//			response.setStatus(ILLEGAL_PARAM.getStatus().value());
//			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "设备号不能为空");
//		}
//		else if (exception instanceof AccountDisabledException) {
//			response.setStatus(ACCOUNT_DYSABLE.getStatus().value());
//			errorMsg = new ErrorMsg(ACCOUNT_DYSABLE, "账号禁用,请联系客服");
		}else{
			response.setStatus(SERVER_ERROR.getStatus().value());
			errorMsg = SERVER_ERROR;
		}
		ServletTool.writeJSONStr(response, objectMapper.writeValueAsString(errorMsg));
	}
	
}
